package com.tuteng.administrator.ashospital.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.tuteng.administrator.ashospital.JsonCallback;
import com.tuteng.administrator.ashospital.MainActivity;
import com.tuteng.administrator.ashospital.R;
import com.tuteng.administrator.ashospital.application.hospitalAppication;
import com.tuteng.administrator.ashospital.beans.BindCardResponse;
import com.tuteng.administrator.ashospital.beans.LandReqBean;
import com.tuteng.administrator.ashospital.beans.UserBean;
import com.tuteng.administrator.ashospital.comprehensive.LoadingDialogManager;

import java.util.ArrayList;

import cn.idchecker.check.Checker;
import cn.jpush.android.api.JPushInterface;

import static com.tuteng.administrator.ashospital.Config.GlobleConfig.APP_URL;
import static com.tuteng.administrator.ashospital.Manager.DateUtils.converttimetoYYMMDD;
import static com.tuteng.administrator.ashospital.comprehensive.checkutils.IDCardNoToBirthday;
import static com.tuteng.administrator.ashospital.comprehensive.checkutils.checkLength;
import static com.tuteng.administrator.ashospital.comprehensive.checkutils.checkLengthofidcard;
import static com.tuteng.administrator.ashospital.comprehensive.checkutils.checkLengthofphone;

/**
 * 开发人：黄启位
 * 时间：2016-06-27
 * 功能描述：用户注册
 */
public class UserRegisterActivity extends BaseActivity implements View.OnClickListener{
    //返回安妮
private ImageButton mImageBack;
    private LinearLayout IncludeTitle;
    //标题
    private TextView mTextTitle;
    /** mEditBrxm:姓名
     * mEditPhone：电话号码
     * mEditSfzh：身份证号
     * mEditJtzz：家庭住址
     */
    private EditText mEditBrxm,mEditPhone,mEditSfzh,mEditJtzz,mEditpass;
    //提交
    private Button mBtntj;

    //注册电话号码
  private   String dhhm;

    //application
    private hospitalAppication app;
    @Override
    protected void initEnvironment() {

    }






    @Override
    protected void initViews() {
        IncludeTitle=(LinearLayout)findViewById(R.id.register_title);
        mImageBack=(ImageButton)IncludeTitle.findViewById(R.id.img_title_back);
        mTextTitle=(TextView)IncludeTitle.findViewById(R.id.tv_include_title);
        mTextTitle.setText(R.string.register_uc_myfriend_title);
        mImageBack.setOnClickListener(this);
        mEditBrxm=(EditText)findViewById(R.id.register_edittext_name);
        mEditJtzz=(EditText)findViewById(R.id.uc_info_hint_jtzz);
        mEditPhone=(EditText)findViewById(R.id.register_edittext_phone);
        mEditSfzh=(EditText)findViewById(R.id.register_edittext_sfzh);
        mBtntj=(Button)findViewById(R.id.register_button_login);
        mEditpass=(EditText)findViewById(R.id.uc_info_hint_mm);
        mBtntj.setOnClickListener(this);
        app=hospitalAppication.getInstance();
    }

    @Override
    protected int getLayoutId() {
        return (R.layout.activity_user_register);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_title_back:
                finish();break;
            case R.id.register_button_login:
                initdata();
                break;
            default:break;
        }

    }

    /**
     * 提交数据
     */
    private void initdata() {
        String brxm=mEditBrxm.getText().toString();
        String sfzh=mEditSfzh.getText().toString();
        String jtzz=mEditJtzz.getText().toString();
         dhhm=mEditPhone.getText().toString();
        String pass=mEditpass.getText().toString();
        String jiguan=null;
        String sex=null;
        String sex1=null;
        String birthdy=null;
         if(checkLengthofidcard(mEditSfzh))
         {if(  checkLengthofphone( mEditPhone))
         {if(checkLength(mEditpass,"密码"))
         { Checker checker = new Checker(sfzh);
          if(checker.check())
          {   String rid = JPushInterface.getRegistrationID(getApplicationContext());
              birthdy=IDCardNoToBirthday(sfzh);
              jiguan=checker.getAddr();
            sex=checker.getSex();
            if(sex.equals("男")){sex1="1";}else {sex1="2";}
              UserBean userBean=new UserBean();
              userBean.setUsername(brxm);
              userBean.setSex(sex1);
              userBean.setRegistrationID(rid);
              userBean.setPassword(pass);
              userBean.setPhone(dhhm);
              userBean.setJtzz(jtzz);
              userBean.setBirthday(birthdy);
              userBean.setProvince(jiguan);
              userBean.setIdcard(sfzh);
              userBean.setMethod("1");
              String neturl=APP_URL+"/Regist";
              String js= JSON.toJSONString(userBean);

              OkGo.<BindCardResponse>post(neturl)
                      .tag(this)
                      .upJson(js)
                      .execute(new JsonCallback<BindCardResponse>() {
                          @Override
                          public void onStart(Request<BindCardResponse, ? extends Request> request) {
                              LoadingDialogManager.getInstance().showDialog();
                          }

                          @Override
                          public void onFinish() {
                              LoadingDialogManager.getInstance().dismissDialog();
                          }

                          @Override
                          public void onSuccess(Response<BindCardResponse> response) {
                              BindCardResponse u=response.body();
                              if(u.getResultCode().equals("0"))
                              {  Toast.makeText(getApplicationContext(),u.getResultMsg(),Toast.LENGTH_LONG).show();
                                  app.setResponse(u);
                                  startActivity(new Intent(UserRegisterActivity.this, MainActivity.class));
                              }
                              else {
Toast toast=Toast.makeText(getApplicationContext(), u.getResultMsg(), Toast.LENGTH_SHORT);
 toast.show();
                              }

                          }
                      });
          }

          else {Toast.makeText(this,"身份证校验不合法请重新填写！",Toast.LENGTH_LONG).show();
              mEditSfzh.requestFocus();
          }
         }

         }
         };




    }
}
